<template>
	<xn-form-container
		:title="formData.id ? '编辑模块' : '增加模块'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="所属产品" name="productCode">
				<ProductSelect v-model:value="formData.productCode" />
			</a-form-item>
			<a-form-item label="所属项目" name="projectCode">
				<ProjectSelect v-model:value="formData.projectCode" />
			</a-form-item>
			<a-form-item label="所属公司" name="deptId">
				<DeptSelect v-model:value="formData.deptId" />
			</a-form-item>
			<a-form-item label="批次" name="batch">
				<a-input v-model:value="formData.batch" placeholder="请输入批次" allow-clear />
			</a-form-item>
			<a-form-item label="所属月份" name="belongingMonth">
				<a-select
					v-model:value="formData.belongingMonth"
					placeholder="请选择所属月份"
					:options="monthOptions"
					allowClear
				/>
			</a-form-item>
			<a-form-item label="结佣比例" name="commRatio">
				<a-input v-model:value="formData.commRatio" placeholder="请输入结佣比例" allow-clear />
			</a-form-item>
			<a-form-item label="委案金额" name="entrustAmt">
				<a-input v-model:value="formData.entrustAmt" placeholder="请输入委案金额" allow-clear />
			</a-form-item>
			<a-form-item label="委案户数" name="entrustCnt">
				<a-input v-model:value="formData.entrustCnt" placeholder="请输入委案户数" allow-clear />
			</a-form-item>
			<a-form-item label="人力" name="staffCnt">
				<a-input v-model:value="formData.staffCnt" placeholder="请输入人力" allow-clear />
			</a-form-item>
			<a-form-item label="回款任务目标" name="repayTarget">
				<a-input v-model:value="formData.repayTarget" placeholder="请输入回款任务目标" allow-clear />
			</a-form-item>
			<a-form-item label="佣金目标" name="commTarget">
				<a-input v-model:value="formData.commTarget" placeholder="请输入佣金目标" allow-clear />
			</a-form-item>
			<!-- stored字段在新增时不展示，修改时设为禁用 -->
			<a-form-item label="上月委案金额" name="lastEntrustAmt">
				<a-input v-model:value="formData.lastEntrustAmt" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="上月委案户数" name="lastEntrustCnt">
				<a-input v-model:value="formData.lastEntrustCnt" placeholder="0" allow-clear disabled />
			</a-form-item>
			<a-form-item label="上月人力" name="lastStaffCnt">
				<a-input v-model:value="formData.lastStaffCnt" placeholder="0" allow-clear disabled />
			</a-form-item>
			<a-form-item label="分总上报回款" name="branchRepay">
				<a-input v-model:value="formData.branchRepay" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="累计回款" name="totalRepay">
				<a-input v-model:value="formData.totalRepay" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="昨日累计" name="yesterdayRepay">
				<a-input v-model:value="formData.yesterdayRepay" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="上月累计回款" name="lastTotalRepay">
				<a-input v-model:value="formData.lastTotalRepay" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="上月目标" name="lastRepayTarget">
				<a-input v-model:value="formData.lastRepayTarget" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="累计垫付" name="totalAdvance">
				<a-input v-model:value="formData.totalAdvance" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="上月垫付" name="lastAdvance">
				<a-input v-model:value="formData.lastAdvance" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item label="上月结佣比例" name="lastCommRatio">
				<a-input v-model:value="formData.lastCommRatio" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="人均载案量" name="perEntrustCnt">
				<a-input v-model:value="formData.perEntrustCnt" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="委案金额环比差异" name="entrustAmtMomDiff">
				<a-input v-model:value="formData.entrustAmtMomDiff" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="上月人均载案量" name="lastPerEntrustCnt">
				<a-input v-model:value="formData.lastPerEntrustCnt" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="回款差" name="repayDiff">
				<a-input v-model:value="formData.repayDiff" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="新增回款" name="addRepay">
				<a-input v-model:value="formData.addRepay" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="任务完成率" name="taskCompleteRate">
				<a-input v-model:value="formData.taskCompleteRate" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="上月任务完成率" name="lastTaskCompleteRate">
				<a-input v-model:value="formData.lastTaskCompleteRate" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="任务完成率差异" name="tcrMomDiff">
				<a-input v-model:value="formData.tcrMomDiff" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="累计达成环比差异" name="repayMomDiff">
				<a-input v-model:value="formData.repayMomDiff" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="累计创拥" name="totalComm">
				<a-input v-model:value="formData.totalComm" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="佣金达成率" name="commCompleteRate">
				<a-input v-model:value="formData.commCompleteRate" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="净累计创拥" name="netComm">
				<a-input v-model:value="formData.netComm" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="昨日累计创拥" name="yesterdayComm">
				<a-input v-model:value="formData.yesterdayComm" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="昨日净创拥" name="yesterdayNet">
				<a-input v-model:value="formData.yesterdayNet" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="上月累计创拥" name="lastTotalComm">
				<a-input v-model:value="formData.lastTotalComm" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="创佣差异" name="commMomDiff">
				<a-input v-model:value="formData.commMomDiff" placeholder="0.00" allow-clear disabled />
			</a-form-item>
			<a-form-item v-if="formData.id" label="上月净累计创拥" name="lastNetComm">
				<a-input v-model:value="formData.lastNetComm" placeholder="0.00" allow-clear disabled />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { ref, defineEmits, defineExpose } from 'vue'
	import { required } from '@/utils/formRules'
	import planApi from '@/api/bi/plan/planApi'
	import tool from '@/utils/tool'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import ProductSelect from '@/components/Tjzy/Product/index.vue'
	import DeptSelect from '@/components/Tjzy/Dept/index.vue'

	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const monthOptions = tool.dictList('month')
	// 默认要校验的
	const formRules = {
		productCode: [required('请输入产品编码')],
		projectCode: [required('请输入项目编码')],
		deptId: [required('请输入所属分公司')],
		batch: [required('请输入批次')],
		belongingMonth: [required('请输入所属月份')],
		commRatio: [required('请输入结佣比例')],
		entrustAmt: [required('请输入委案金额')],
		entrustCnt: [required('请输入委案户数')],
		staffCnt: [required('请输入人力')],
		repayTarget: [required('请输入回款任务目标')],
		commTarget: [required('请输入佣金目标')]
	}

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			formData.value = Object.assign({}, record)
		}
	}

	// 关闭抽屉
	const onClose = () => {
		formData.value = Object.assign({}, {})
		visible.value = false
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				if (formData.value.id) {
					planApi.edit(formData.value).then(() => {
						onClose()
						emit('successful')
					})
				} else {
					planApi.add(formData.value).then(() => {
						onClose()
						emit('successful')
					})
				}
			})
			.catch(() => {})
	}

	defineExpose({
		onOpen
	})
</script>
